﻿document.getElementsByClassName = function(cl) {
    var retNode = [];
    var myClass = new RegExp('\\b' + cl + '\\b');
    var element = this.getElementsByTagName('*');
    for (var i = 0; i < element.length; i++) {
        var classes = element[i].className;
        if (myClass.test(classes)) retNode.push(element[i]);
    }
    return retNode;
};

function rcToggle(event, divId, skin, collapseImg, expandImg) {
    var toggleImg = (event.srcElement) ? event.srcElement : event.target;
    var contentDiv = document.getElementById(divId);
    
    if (event.ctrlKey == false) {
        toggleRcDiv(contentDiv, skin);
        toggleRcImage(toggleImg, collapseImg, expandImg, skin);
    } else {
    toggleAllRcDivs(contentDiv.className, toggleImg.className, collapseImg, expandImg, skin);
    }
}

function toggleAllRcDivs(divClass, imgClass, collapseImg, expandImg, skin) {
    var divs = document.getElementsByClassName(divClass);
    var imgages = document.getElementsByClassName(imgClass);

    for (i = 0; i < divs.length; i++) {
        var collapsible = divs[i].getAttribute("collapsible");
        if (collapsible == "True")
            toggleRcDiv(divs[i], skin);
    }

    for (x = 0; x < imgages.length; x++) {
        toggleRcImage(imgages[x], collapseImg, expandImg, skin);
    }
}

function toggleRcDiv(element, skin) {
    element.className = (element.className.toLowerCase().indexOf('hide') > -1) ? skin + 'RcContent' : skin + 'RcHide';
}

function toggleRcImage(toggleImg, collapseImg, expandImg, skin) {
    toggleImg.src = (toggleImg.className.toLowerCase().indexOf('hide') > -1) ? expandImg : collapseImg;
    toggleImg.className = (toggleImg.className.toLowerCase().indexOf('hide') > -1) ? skin + 'RcToggleShow' : skin + 'RcToggleHide';
}